#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>


struct ListNode {
    int val;
    struct ListNode* next;
    
};
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if (list1 == NULL)
    {
        return list2;
    }
    if (list2 == NULL)
    {
        return list1;
    }
    ListNode* l1, * l2;
    ListNode* newhead, * newtail;
    l1 = list1;
    l2 = list2;
    newhead = newtail = NULL;
    while (l1 && l2)
    {
        if (l1->val < l2->val)
        {
            if (newhead == NULL)
            {
                newhead = newtail = l1;
            }
            else
            {
                newtail->next = l1;
                newtail = newtail->next;
            }
            l1 = l1->next;
        }
        else
        {
            if (newhead == NULL)
            {
                newhead = newtail = l2;
            }
            else
            {
                newtail->next = l2;
                newtail = newtail->next;
            }
            l2 = l2->next;
        }
    }
    if (l1)
    {
        newtail->next = l1;
    }
    if (l2)
    {
        newtail->next = l2;
    }
    return newhead;
}